#ifndef RBT_FILE_MAPPING_STREAM
#define RBT_FILE_MAPPING_STREAM

#include "StreamConvert.h"
#include <Windows.h>
RETURNCODE E_OPEN_FILE_MAPPING         = -501;
RETURNCODE E_FILE_MAPPING              = -502;
RETURNCODE E_FILEPOSITION_MAPPING      = -503;
RETURNCODE E_FILESIZE_MAPPING          = -504;
RETURNCODE E_FILESEEK_MAPPING          = -505;
RETURNCODE E_FILEREAD_MAPPING          = -506;
RETURNCODE E_FILEWRITE_MAPPING         = -507;

class FileMappingStream : 
	public StreamConvert< FileMappingStream >
{
public:
	FileMappingStream(void);
	virtual ~FileMappingStream(void);

	enum MAPPING_MODE
	{
		MODE_READ_ONLY,
		MODE_READ_WRITE,
	};

	int32 Open(ctchar* pcsFilePath, uint64 uFileSize, uint64 uMappingPos = 0 * 64 * KB, uint64 uMappingLen = 0, uint32 uMode = MODE_READ_ONLY, ctchar* pcsShareName = NULL);
	virtual int32 CanRead();
	virtual int32 CanSeek();
	virtual int32 CanWrite();
	virtual int32 HaveEOS();

	virtual int32 Seek( uint64 position , SEEK_POSITION flag = CURRENT );
	virtual int32 GetPosition( uint64& position );
	virtual int32 GetSize( uint64& size );
	virtual int32 Read(  byte* block , uint32 blockSize , uint32& fileRead );
	virtual int32 Write( byte* block , uint32 blockSize , uint32& fileRead );
	virtual int32 EndOfStream();
	virtual int32 Close();
	virtual int32 Flush();

protected:
	int32 CheckFileMappingInvalidate();

	HANDLE m_hFile;
	HANDLE m_hFileMapping;
	byte*  m_pFileMapping;
	uint64 m_uFileSize;
	uint64 m_uBeginPos;
	uint64 m_uCurrentPos;
	uint64 m_uEndPos;
	uint64 m_uMappingLen;
};

#endif
